package com.kurumi.autoprint.service.impl.order;

import java.util.Date;
import java.util.List;

import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kurumi.autoprint.enums.OrderStatusEnum;
import com.kurumi.autoprint.mapper.MrPrintDetailMapper;
import com.kurumi.autoprint.mapper.mapper.print.PrintMrOrderMapper;
import com.kurumi.autoprint.pojo.MrPrintDetail;
import com.kurumi.autoprint.pojo.print.PrintMrOrder;
import com.kurumi.autoprint.service.order.PrintMrOrderService;
import com.kurumi.autoprint.util.Result;

@Transactional
@Service
public class PrintMrOrderServiceImpl implements PrintMrOrderService{
	
	@Autowired
	private PrintMrOrderMapper printMrOrderMapper;

	@Autowired
	private MrPrintDetailMapper mrPrintDetailMapper;
	
	@Override
	public Result insertPrintMrOrder(PrintMrOrder printMrOrder, MrPrintDetail mrPrintDetail) {
		int result =  printMrOrderMapper.insertPrintMrOrder(printMrOrder);
		
		if (result == 0) {
			return new Result(false, "添加打印费用订单到数据库异常!", null, 500);
		}
		
		mrPrintDetail.setMrPrintOrderId(printMrOrder.getId());
		mrPrintDetail.setCreateTime(new Date());
				
		if (mrPrintDetailMapper.insertSelective(mrPrintDetail) == 0) {
			return new Result(false, "添加订单病案信息到数据库异常!", null, 500);
		}
		
		return new Result(true, "添加成功!", null, 200);
	}


	
	@Override
	public int updatePrintMrOrderStatusByBillNo(String billNo,Integer statsu) {
		
		List<PrintMrOrder> printMrOrderList = printMrOrderMapper.selectByOrderNum(billNo);
		if(printMrOrderList!=null && printMrOrderList.size()>0) {
			PrintMrOrder printMrOrder = printMrOrderList.get(0);
			printMrOrder.setNotifyTime(new Date());
			printMrOrder.setStatus(statsu);
			return printMrOrderMapper.updateByPrimaryKey(printMrOrder);
		}
		
		return 0;
	}
	
	@Override
	public PrintMrOrder getprintMrOrderByOrderNum(String orderNum) {
		List<PrintMrOrder> printMrOrderList = printMrOrderMapper.selectByOrderNum(orderNum);
		if(CollectionUtils.isEmpty(printMrOrderList)) {
			return null;
		}
		return printMrOrderList.get(0);
	}


	/**
	 * 修改打印状态
	 */
	@Override
	public int updatePrintStatusByOrderNum(String orderNum, Integer printStatus, String printErrorMsg) {
		return printMrOrderMapper.updatePrintStatusByOrderNum(orderNum, printStatus, printErrorMsg);
	
	}
	
	
	
	
	
	
	
	
	

}
